﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SEO.Utility
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 本类主要封装了对股票日常通用操作相关的功能
    /// 完成日期：2011年4月14日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// 
    /// 修改记录1: 
    /// 修改日期：2011年4月15日
    /// 版 本 号：V1.1
    /// 修 改 人：Adin
    /// 修改内容：对代码规范进行修改，使代码更容易阅读，更容易维护
    /// </summary>
    public class StringHelper
    {
        /// <summary>
        /// 清除HTML代码
        /// </summary>
        /// <param name="strHtmlContent">HTML代码内容</param>
        /// <returns>返回清理过HTML代码的内容</returns>
        public static string CleanHtml(string strHtmlContent)
        {
            Regex rx = new Regex("<[^>]*>");
            return rx.Replace(strHtmlContent, "");
        }

        /// <summary>
        /// 清除A标签
        /// </summary>
        /// <param name="strContent">需要清除A标签的内容</param>
        /// <returns>返回没有A标签的内容</returns>
        public static string ClearATag(string strContent)
        {
            strContent = strContent.ToLower();//转为小写字母

            Regex rx = new Regex("<a[^>]+>(.+?)<\\/a>");
            strContent = rx.Replace(strContent, "$1");

            return strContent;
        }

        /// <summary>
        /// 删除Script及Iframe脚本
        /// </summary>
        /// <param name="strHtmlString">内容</param>
        /// <returns>返回删除Script及Iframe脚本内容</returns>
        public static string ClearIframeAndScript(string strHtmlString)
        {
            strHtmlString = Regex.Replace(strHtmlString, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            strHtmlString = Regex.Replace(strHtmlString, @"<iframe[^>]*?>.*?</iframe>", "", RegexOptions.IgnoreCase);
            return strHtmlString;
        }

        /// <summary>
        /// 清除所有HTML标签
        /// </summary>
        /// <param name="strHtmlContent">带有HTML标签的内容</param>
        /// <returns>返回没有HTML标签的内容</returns>
        public static string ClearAllHtmlTag(string strHtmlContent)
        {
            System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"<script[/s/S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex rx2 = new System.Text.RegularExpressions.Regex(@" href *= *[/s/S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex rx3 = new System.Text.RegularExpressions.Regex(@" on[/s/S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex rx4 = new System.Text.RegularExpressions.Regex(@"<iframe[/s/S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex rx5 = new System.Text.RegularExpressions.Regex(@"<frameset[/s/S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            strHtmlContent = rx1.Replace(strHtmlContent, ""); //过滤<script></script>标记 
            strHtmlContent = rx2.Replace(strHtmlContent, ""); //过滤href=javascript: (<A>) 属性 
            strHtmlContent = rx3.Replace(strHtmlContent, " _disibledevent="); //过滤其它控件的on...事件 
            strHtmlContent = rx4.Replace(strHtmlContent, ""); //过滤iframe 
            strHtmlContent = rx5.Replace(strHtmlContent, ""); //过滤frameset 
            return strHtmlContent;
        }

        /// <summary>
        /// 清除文章中的HTML特殊标签，通常用于显示文章内容页面
        /// </summary>
        /// <param name="strContent">文章内容</param>
        /// <returns>返回清除过文章中的HTML特殊标签的内容</returns>
        public static string ClearHTMLTagForArticle(string strContent)
        {
            strContent = strContent.Replace("<br>", "[#]");
            strContent = strContent.Replace("<BR>", "[#]");
            strContent = strContent.Replace("</br>", "[/#]");
            strContent = strContent.Replace("</BR>", "[/#]");
            strContent = strContent.Replace("<p>", "[$]");
            strContent = strContent.Replace("</p>", "[/$]");
            strContent = strContent.Replace("<P>", "[$]");
            strContent = strContent.Replace("</P>", "[/$]");
            strContent = strContent.Replace("<img", "[$img$]");
            strContent = strContent.Replace("<IMG", "[$img$]");
            strContent = strContent.Replace("\r", "[$\r$]");
            strContent = strContent.Replace("\n", "[$\n$]");
            strContent = strContent.Replace("<br />", "[$br$]");

            strContent = CleanHtml(strContent);

            strContent = strContent.Replace("[#]", "<br>");
            strContent = strContent.Replace("[/#]", "</br>");
            strContent = strContent.Replace("[$]", "<p>");
            strContent = strContent.Replace("[/$]", "</p>");
            strContent = strContent.Replace("&quot;", "\"");
            strContent = strContent.Replace("&amp;quot;", "\"");
            strContent = strContent.Replace("[$img$]", "<img");
            strContent = strContent.Replace("&ldquo;", "“");
            strContent = strContent.Replace("&rdquo;", "”");
            strContent = strContent.Replace("&amp;ldquo;", "“");
            strContent = strContent.Replace("&amp;rdquo;", "”");
            strContent = strContent.Replace("&amp;middot;", ".");
            strContent = strContent.Replace("&mdash;", "—");
            strContent = strContent.Replace("&amp;&mdash;", "—");
            strContent = strContent.Replace("&rarr;", "→");
            strContent = strContent.Replace("&amp;&rarr;", "→");
            strContent = strContent.Replace("[$\r$]", "\r");
            strContent = strContent.Replace("[$\n$]", "\n");
            strContent = strContent.Replace("[$br$]", "<br />");

            return strContent;
        }

        /// <summary>
        /// 清除特殊HTML标签
        /// </summary>
        /// <param name="strHtmlstring">内容</param>
        /// <returns>返回清除完特殊HTML标签的内容</returns>
        public static string ClearSpecialHTML(string strHtmlstring)
        {
            //删除脚本
            strHtmlstring = Regex.Replace(strHtmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            strHtmlstring = Regex.Replace(strHtmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"-->", "", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            strHtmlstring = Regex.Replace(strHtmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            strHtmlstring.Replace("<", "");
            strHtmlstring.Replace(">", "");
            strHtmlstring.Replace("\r\n", "");
            strHtmlstring = System.Web.HttpContext.Current.Server.HtmlEncode(strHtmlstring).Trim(); 
            return strHtmlstring;
        }

        /// <summary>
        /// 转半角的函数(SBC case)
        /// </summary>
        /// <param name="strInput">输入字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToDBC(string strInput)
        {
            char[] arch = strInput.ToCharArray();
            for (int i = 0; i < arch.Length; i++)
            {
                if (arch[i] == 12288)
                {
                    arch[i] = (char)32;
                    continue;
                }
                if (arch[i] > 65280 && arch[i] < 65375)
                {
                    arch[i] = (char)(arch[i] - 65248);
                }
            }
            return new string(arch);
        }

        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="strInput">任意字符串</param>
        /// <returns>全角字符串</returns>
        /// <remarks>
        /// 全角空格为12288，半角空格为32
        /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        /// </remarks>        
        public static string ToSBC(string strInput)
        {
            //半角转全角：
            char[] arch = strInput.ToCharArray();
            for (int i = 0; i < arch.Length; i++)
            {
                if (arch[i] == 32)
                {
                    arch[i] = (char)12288;
                    continue;
                }
                if (arch[i] < 127)
                {
                    arch[i] = (char)(arch[i] + 65248);
                }
            }
            return new string(arch);
        }

        /// <summary>
        /// 格式化显示价格数据，如：1000000.00 格式化后为：1,000,000.00
        /// </summary>
        /// <param name="strPrice">价格</param>
        /// <returns>返回格式化字符串</returns>
        public static string FormatPriceViewData(string strPrice)
        {
            string strReturnValue = "";
            string strNewPrice = strPrice;
            if (!strPrice.Contains("."))
            {
                strPrice = strPrice + ".00";
            }

            if (strPrice.Contains("-"))
            {
                strPrice = strPrice.Replace("-", "");
            }

            string[] splitPrice = strPrice.Split(new char[] { '.' });
            string strFormatPrice = splitPrice[0].ToString();
            int nLength = strFormatPrice.Length;
            string strGetFormatPrice = "";
            switch (nLength)
            {
                case 3: strGetFormatPrice = strFormatPrice;
                    break;
                case 4: strGetFormatPrice = strFormatPrice.Insert(1, ",");
                    break;
                case 5: strGetFormatPrice = strFormatPrice.Insert(2, ",");
                    break;
                case 6: strGetFormatPrice = strFormatPrice.Insert(3, ",");
                    break;
                case 7:
                    strGetFormatPrice = strFormatPrice.Insert(1, ",");
                    strGetFormatPrice = strFormatPrice.Insert(5, ",");
                    break;
                case 8:
                    //10000000.00 -> 10,000,000.00
                    strGetFormatPrice = strFormatPrice.Insert(2, ",");
                    strGetFormatPrice = strGetFormatPrice.Insert(6, ",");
                    break;
                case 9:
                    //100000000.00 -> 100,000,000.00
                    strGetFormatPrice = strFormatPrice.Insert(3, ",");
                    strGetFormatPrice = strGetFormatPrice.Insert(7, ",");
                    break;
                case 10:
                    //1000000000.00 -> 1,000,000,000.00
                    strGetFormatPrice = strFormatPrice.Insert(1, ",");
                    strGetFormatPrice = strGetFormatPrice.Insert(5, ",");
                    strGetFormatPrice = strGetFormatPrice.Insert(9, ",");
                    break;
                default:
                    strGetFormatPrice = strFormatPrice;
                    break;

            }

            strReturnValue = strGetFormatPrice + ".00";
            if (strNewPrice.Contains("-"))
            {
                strReturnValue = "-" + strReturnValue;
            }

            return strReturnValue;
        }

        /// <summary>
        /// 将可能存在空值的对象转换为空串
        /// </summary>
        /// <param name="obj">对象类型</param>
        /// <returns></returns>
        public static string NullToString(object obj)
        {
            if (obj == null)
            {
                return "";
            }
            return obj.ToString();
        }

        /// <summary>
        /// 将空值或空串转换为默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="strDefaultValue"></param>
        /// <returns></returns>
        public static string NullToString(object obj, string strDefaultValue)
        {
            if (obj == null || obj.ToString().Equals(""))
            {
                return strDefaultValue;
            }
            return obj.ToString();
        }

        /// <summary>
        /// 将可能存在空值数据用默认值替换,转入数据需要能转换为int型
        /// 此该当主要用来将数据库中可能存在空值的数据的显示，后续可做改进，如把不能转换为INT型的也返回默认值等。
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int NullToInt(object obj, int defaultValue)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return defaultValue;
            }
            return Convert.ToInt32(obj);
        }

        /// <summary>
        /// 格式化显示数字
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="num">小数位数</param>
        /// <param name="nullStr">为空值或空串时显示的值</param>
        /// <param name="zeroStr">为0时显示的值</param>
        /// <returns></returns>
        public static string FormatDecimal(object objValue, int num, string nullStr, string zeroStr)
        {
            if (objValue != null && (!objValue.ToString().Equals("")))
            {
                if ((Convert.ToDecimal(objValue)) == 0)
                {
                    return zeroStr;
                }
                else
                {
                    string formatstr = "";
                    for (int i = 0; i < num; i++)
                    {
                        formatstr += "0";
                    }
                    return (Convert.ToDecimal(objValue)).ToString("N" + num.ToString());
                }
            }
            return nullStr;
        }

        /// <summary>
        /// 格式化显示数字，非货币形式（即字符之间不需要逗号开）
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="num">小数位数</param>
        /// <param name="nullStr">为空值或空串时显示的值</param>
        /// <param name="zeroStr">为0时显示的值</param>
        /// <returns></returns>
        public static string FormatDecimalNoComma(object objValue, int num, string nullStr, string zeroStr)
        {
            if (objValue != null && (!objValue.ToString().Equals("")))
            {
                if ((Convert.ToDecimal(objValue)) == 0)
                {
                    return zeroStr;
                }
                else
                {
                    string formatstr = "";
                    for (int i = 0; i < num; i++)
                    {
                        formatstr += "0";
                    }
                    return (Convert.ToDecimal(objValue)).ToString("F" + num.ToString());
                }
            }
            return nullStr;
        }

        /// <summary>
        /// 格式化显示数字(格式化为num位小数,不用","分隔)
        /// </summary>
        /// <remarks>
        /// klz:20091218 add
        /// </remarks>
        /// <param name="objValue">要转换的值</param>
        /// <param name="num">小数位数</param>
        /// <returns></returns>
        public static string FormatDecimal(object objValue, int num, string nullStr)
        {
            if (objValue != null && (!objValue.ToString().Equals("")))
            {
                string strValue = objValue.ToString();
                decimal dResult;
                bool b = decimal.TryParse(strValue, out dResult);
                if (b)
                {
                    return Math.Round(dResult, num).ToString();
                }
                else
                {
                    return null;
                }
            }
            return nullStr;
        }

        /// <summary>
        /// 格式化显示百分比数字
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="num">小数位数</param>
        /// <param name="nullStr">为空值或空串时显示的值</param>
        /// <param name="zeroStr">为0时显示的值</param>
        /// <returns></returns>
        public static string FormatDecimalPercent(object objValue, int num, string nullStr, string zeroStr)
        {
            if (objValue != null && (!objValue.ToString().Equals("")))
            {
                if (((decimal)objValue) == 0)
                {
                    return zeroStr;
                }
                else
                {
                    string formatstr = "";
                    for (int i = 0; i < num; i++)
                    {
                        formatstr += "0";
                    }
                    return ((decimal)objValue).ToString("F" + num.ToString()) + "%";
                }
            }
            return nullStr;
        }


        /// <summary>
        /// 转OBJ为DECIMAL
        /// </summary>
        /// <param name="objValue"></param>
        /// <returns></returns>
        public static decimal ConvertToDecimal(object objValue)
        {
            if (objValue != null && (!objValue.ToString().Equals("")))
            {
                return (Convert.ToDecimal(objValue));
            }
            return 0;
        }

        /// <summary>
        /// 转OBJ为Double
        /// </summary>
        /// <param name="objValue"></param>
        /// <returns></returns>
        public static double ConvertToDouble(object objValue)
        {
            if (objValue != null && (!objValue.ToString().Equals("")))
            {
                return (Convert.ToDouble(objValue));
            }
            return 0;
        }

        /// <summary>
        /// 取最大值，都将转换为Double处理
        /// </summary>
        /// <param name="lstObj"></param>
        /// <returns></returns>
        public static decimal GetMax(List<decimal> lstObj)
        {
            decimal retvalue = decimal.MinValue;
            for (int i = 0; i < lstObj.Count; i++)
            {
                retvalue = Math.Max(retvalue, lstObj[i]);
            }
            return retvalue;
        }

        /// <summary>
        /// 取最小值，都将转换为Double处理
        /// </summary>
        /// <param name="lstObj"></param>
        /// <returns></returns>
        public static decimal GetMin(List<decimal> lstObj)
        {
            decimal retvalue = decimal.MaxValue;
            for (int i = 0; i < lstObj.Count; i++)
            {
                retvalue = Math.Min(retvalue, lstObj[i]);
            }
            return retvalue;
        }

        /// <summary>
        /// 获取标准格式的时间
        /// </summary>
        /// <param name="strDateTime">新浪微博时间</param>
        /// <returns>返回时间</returns>
        public static DateTime GetDateTime(string strDateTime)
        {
            System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            string format = "ddd MMM d HH:mm:ss zz00 yyyy";
            string stringValue = DateTime.Now.ToString(format, cultureInfo); // 得到日期字符串
            DateTime datetime = DateTime.ParseExact(strDateTime, format, cultureInfo); // 将字符串转换成日期

            return datetime;
        }

        /// <summary>
        /// 计算字符串长度，中文及全角算1，其它算0.5
        /// </summary>
        /// <param name="strText">字符串</param>
        /// <returns>返回字符串长度</returns>
        public static int GetStringLen(string strText)
        {
            int iLen = 0;
            //记录非中文、非全角字符为0.5个长度
            float fCharLen = 0;
            char[] chars = strText.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                int iCharLength = Encoding.UTF8.GetByteCount(chars[i].ToString());
                if (iCharLength == 3)
                {
                    iLen++;
                }
                else
                {
                    if (fCharLen == 0.5)
                    {
                        fCharLen = 0;
                    }
                    else
                    {
                        fCharLen = 0.5f;
                        iLen++;
                    }
                }
            }

            return iLen;
        }

        /// <summary>
        /// 将超链接字符串拆分为链接和文字两部分
        /// </summary>
        /// <param name="strLinkText">字符串链接</param>
        /// <returns>返回链接</returns>
        public static string GetLinkAndText(string strLinkText)
        {
            Regex re = new Regex(@"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match m = re.Match(strLinkText);
            return m.Groups[0].ToString();
        }

        /// <summary>
        /// 格式化网站域名格式
        /// </summary>
        /// <param name="strUrl">网站网址</param>
        /// <returns>返回格式化后的域名</returns>
        public static string GetFormatURL(string strUrl)
        {
            //www.chinamyhosting.com/seoblog/
            //finance.sina.com.cn/stock/

            string strFormatUrl = "";

            try
            {
                //匹配到网址
                string[] splitUrl = strUrl.Split(new char[] { '/' });
                string strGetUrl = splitUrl[0].ToString();
                strFormatUrl = strGetUrl;
                ////匹配到不带www域名
                //string[] splitDomain = strGetUrl.Split(new char[] { '.' });
                //if (splitDomain.Length < 2)
                //{
                //    //如:hexun.com
                //    strFormatUrl = splitDomain[0].ToString() + "." + splitDomain[1].ToString();
                //}
                //else if (splitDomain.Length == 2)
                //{
                //    //www.test.com
                //    //a.test.com
                //    //test.com.cn
                //    strFormatUrl = splitDomain[1].ToString() + "." + splitDomain[2].ToString();
                //}
                //else if (splitDomain.Length == 3)
                //{
                //    //www.test.com.cn
                //    //a.test.com.cn
                //    //t.sina.com.cn
                //    //open.t.sohu.com
                //    strFormatUrl = splitDomain[1].ToString() + "." + splitDomain[2].ToString();
                //}
                //else
                //{
                //    strFormatUrl = splitDomain[1].ToString() + "." + splitDomain[2].ToString();
                //}
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("格式化网站域名格式 -> " + ex.Message);
            }

            return strFormatUrl;
        }

        /// <summary>
        /// 判断字符是否是中文
        /// </summary>
        /// <param name="strString">字符</param>
        /// <returns>True or False</returns>
        public static bool IsChina(string strString)
        {
            bool bValue = false;
            for (int i = 0; i < strString.Length; i++)
            {
                if (Convert.ToInt32(Convert.ToChar(strString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
                {
                    bValue = false;
                }
                else
                {
                    return bValue = true;
                }
            }

            return bValue;
        }

        /// <summary>
        /// 对字符串进行GB2312编码
        /// </summary>
        /// <param name="strKeywords">字符串</param>
        /// <returns>返回gb2312编码字符串</returns>
        public static string GetStringGB2312(string strKeywords)
        {
            string strReturnValue = "";
            string strGetGB2312 = "";
            string strNumeric = Regex.Replace(strKeywords, @"[\u4e00-\u9fa5]*", "");//拼音
            string strChinese = Regex.Replace(strKeywords, @"[^\u4e00-\u9fa5]*", "");//汉字 

            //对汉字进行gb2312编码
            Byte[] encodedBytes = new byte[strKeywords.Length * 2];
            int iCount = Encoding.GetEncoding("gb2312").GetBytes(strKeywords, 0, strKeywords.Length, encodedBytes, 0);
            foreach (Byte b in encodedBytes)
            {
                string strValue = Convert.ToString(b, 16);
                strValue = strValue.ToUpper();
                strGetGB2312 += "%" + strValue;
            }

            strGetGB2312 = strNumeric + strGetGB2312;
            strReturnValue = strGetGB2312;

            return strGetGB2312;
        }

        /// <summary>
        /// 匹配电话号码
        /// </summary>
        /// <param name="strContent">内容</param>
        /// <returns>返回电话号码</returns>
        public static string GetPhones(string strContent)
        {
            try
            {
                strContent = Regex.Match(strContent, @"(\d{6,12})|(\d{5}-\d{5})|(\d{3}-\d{3,4}-\d{3,4})|(\d{2,4}-\d{6,9})|(\d{2,4}-\d{2,4}-\d{6,9})").ToString();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("匹配电话号码 -> " + ex.Message);
            }
            return strContent;
        }

        /// <summary>
        /// 获取页面的链接正则
        /// </summary>
        /// <param name="strHtmlCode">字符串</param>
        /// <returns>返回链接</returns>
        public static string GetHref(string strHtmlCode)
        {
            string strUrl = "";
            Match m = Regex.Match(strHtmlCode, @"<a[^>]*href=(?<sin1>['""]?)(?<url>[^'""\s]*)(\k<sin1>)?[^>]*?(title=(?<sin2>['""]?)(?<title>[^'""]*)(\k<sin2>)?[^>]*)?>(?<text>[^<]*)</a>", RegexOptions.IgnoreCase);
            if (m.Success)
            {
                strUrl = m.Groups["url"].Value;
                string strTitle = m.Groups["title"].Value;
                string strText = m.Groups["text"].Value;
            }

            return strUrl;
        }

        /// <summary>
        /// 匹配页面的图片地址
        /// </summary>
        /// <param name="HtmlCode"></param>
        /// <param name="imgHttp">要补充的http://路径信息</param>
        /// <returns></returns>
        public static string GetImgSrc(string HtmlCode, string imgHttp)
        {
            string MatchVale = "";
            string Reg = @"<img.+?>";
            foreach (Match m in Regex.Matches(HtmlCode.ToLower(), Reg))
            {
                MatchVale += GetImg((m.Value).ToLower().Trim(), imgHttp) + "|";
            }
            return MatchVale;
        }

        /// <summary>
        /// 匹配<img src="" />中的图片路径实际链接
        /// </summary>
        /// <param name="ImgString"><img src="" />字符串</param>
        /// <returns></returns>
        public static string GetImg(string ImgString, string imgHttp)
        {
            string MatchVale = "";
            string Reg = @"src=.+\.(bmp|jpg|gif|png|)";
            foreach (Match m in Regex.Matches(ImgString.ToLower(), Reg))
            {
                MatchVale += (m.Value).ToLower().Trim().Replace("src=", "");
            }
            if (MatchVale.IndexOf(".net") != -1 || MatchVale.IndexOf(".com") != -1 || MatchVale.IndexOf(".org") != -1 || MatchVale.IndexOf(".cn") != -1 || MatchVale.IndexOf(".cc") != -1 || MatchVale.IndexOf(".info") != -1 || MatchVale.IndexOf(".biz") != -1 || MatchVale.IndexOf(".tv") != -1)
                return (MatchVale);
            else
                return (imgHttp + MatchVale);
        }

        /// <summary>
        /// 替换通过正则获取字符串所带的正则首尾匹配字符串
        /// </summary>
        /// <param name="RegValue">要替换的值</param>
        /// <param name="regStart">正则匹配的首字符串</param>
        /// <param name="regEnd">正则匹配的尾字符串</param>
        /// <returns></returns>
        public static string RegReplace(string RegValue, string regStart, string regEnd)
        {
            string s = RegValue;
            if (RegValue != "" && RegValue != null)
            {
                if (regStart != "" && regStart != null)
                {
                    s = s.Replace(regStart, "");
                }
                if (regEnd != "" && regEnd != null)
                {
                    s = s.Replace(regEnd, "");
                }
            }
            return s;
        }

    }
}
